#! -*- coding:utf-8 -*-
'''
save html data to mongodb kickchufang table,for data process
'''
import sys, os, time, uuid, re, codecs
import urllib2
import chardet
from bs4 import BeautifulSoup
from pymongo import MongoClient

reload(sys)
sys.setdefaultencoding('utf-8')


def gethtml(url):
    #headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    #req = urllib2.Request(url=url, headers=headers)
    #data = urllib2.urlopen(req).read()
    content = urllib2.urlopen(url).read()
    typeEncode = sys.getfilesystemencoding()  ##系统默认编码
    print chardet.detect(content)
    infoencode = chardet.detect(content).get('encoding', 'utf-8')  ##通过第3方模块来自动提取网页的编码
    html = content.decode(infoencode, 'ignore').encode("GB18030")
    #content =urllib2.urlopen(url).read().decode("gb2312").encode("utf-8") #decode("UTF-8").encode(systype) #data #.decode("UTF-8") #.encode(systype)
    #print html
    return html



#transfer data from kickhtml to kickchufang
def transfer_data(doctorname):
    conn = MongoClient("okzor.com", 27017)
    db = conn.health
    db.authenticate("hou", "hou@123")
    contents = db.kickhtml.find({"author":doctorname})
    for i in contents:
        insert_doc={"uuid": i['uuid'],"doctorId":i["author"],"desc":i["desc"]}
        db.kickchufang.insert(insert_doc)

#transfer data from kickhtml to kickchufang
def transfer_data2(doctorname):
    conn = MongoClient("okzor.com", 27017)
    db = conn.health
    db.authenticate("hou", "hou@123")
    contents = db.kickhtml.find({"author":doctorname})
    for i in contents:
        insert_doc={"uuid": i['uuid'],"doctorId":i["author"],"desc":i["desc"],"title":i["title"]}
        db.kickchufang.insert(insert_doc)


'''
#保存医案到:kickhtml
'''
def saveto_mongo(doctorid,desc,title):
    conn = MongoClient("okzor.com", 27017)
    db = conn.health
    db.authenticate("hou", "hou@123")
    insert_doc={"uuid": str(uuid.uuid1()).replace("-", ""),"author":doctorid,"desc":desc,"title":title}
    db.kickhtml.insert(insert_doc)









